package com.example.sample.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.R.id;
import com.example.sample.control.ScrollerView;

import net.liteheaven.xblib.common.log.XLog;

public class ScrollActivity extends BaseActivity {

    private ScrollerView mIvScroll;
    private Button mBtnAction;
    private LinearLayout mContentView;
    private EditText mEtStep;
    private int mOritation = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        mEtStep = (EditText)findViewById(id.etStep);
        mIvScroll = (ScrollerView)findViewById(R.id.ivScroll);
        mContentView = (LinearLayout)findViewById(id.contentView);
        mBtnAction = (Button)findViewById(R.id.btnAction);
        mBtnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int height = mIvScroll.getHeight();
                int currY = mIvScroll.getScroller().getCurrY();
                int contentHeight = mContentView.getHeight();
                if (Math.abs(currY) + contentHeight > height){
                    mOritation = 1;
                }
                if (currY > 0){
                    mOritation = -1;
                }
                String stepStr = mEtStep.getText().toString();
                int step;
                try {
                    step = Integer.valueOf(stepStr);
                }catch(Exception e){
                    step = 0;
                }
                int offset = step * mOritation;
                XLog.i(TAG, "currY = " + currY + " height = " + height + " offset = " + offset);
                mIvScroll.smoothScrollBy(0, offset);
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean ret = super.onTouchEvent(event);
        return ret;
    }
}
